package com.luoqiu.yygh.cmn.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.luoqiu.yygh.cmn.mapper.DictMapper;
import com.luoqiu.yygh.model.cmn.Dict;
import com.luoqiu.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;

import java.util.Map;

public class EasyExcelListener extends AnalysisEventListener<DictEeVo> {

    private final DictMapper dictMapper;

    public EasyExcelListener(DictMapper dictMapper) {
        this.dictMapper = dictMapper;
    }


    /**
     * 一行一行的读取excel的数据 从第二行开始读取
     * @param dictExcel 读取到的数据
     * @param analysisContext ctx
     */
    @Override
    public void invoke(DictEeVo dictExcel, AnalysisContext analysisContext) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictExcel, dict);
        dictMapper.insert(dict);
    }

    /**
     * 读取完后会执行的方法
     * @param analysisContext ctx
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    /**
     * 读取第一行 表头的数据
     * @param headMap 读取的数据
     * @param context ctx
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        super.invokeHeadMap(headMap, context);
    }
}
